<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Member;
use App\Models\News;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Models\Store;

/**
 * 首页
 *
 * @author Miluo Huang
 */
class GlobalController extends Controller
{

	public function __construct()
	{
		$this->middleware('auth.admin');
	}

	/**
	 * 信息编辑
	 */
	public function infoEdit(Request $request)
	{
        // 验证输入。
        $this->validate($request, [
            'type'       => 'required',
            'ids'    => 'required',
            'from'    => 'required'
        ], [
            'type.required'       => '请选择操作类型！',
            'ids.required' => '请选择操作对象！',
            'from.required' => '请选择操作分类'
        ]);
        $datas = explode(',',$request->input('ids'));
        array_filter($datas);
        switch ($request->input('from')){
            case 'store':
                switch ($request->input('type')){
                    case 1:
                        foreach ($datas as $item){
                            $data = Store::find($item);
                            if (!is_null($data)){
                                $data->status = 1;
                                $data->save();
                            }
                        }
                        break;
                    case 2:
                        foreach ($datas as $item){
                            $data = Store::find($item);
                            if (!is_null($data)){
                                $data->status = 0;
                                $data->save();
                            }
                        }
                        break;
                    case 3:
                        foreach ($datas as $item){
                            $data = Store::find($item);
                            if (!is_null($data)){
                                $data->delete();
                            }
                        }
                        break;
                    case 4:
                        $data = Store::find($request->input('ids'));
                        if (!is_null($data)){
                            $data->sort_at = $request->input('sort_at');
                            $data->save();
                        }
                        break;
                }
                break;
            case 'admin':
                switch ($request->input('type')){
                    case 3:
                        foreach ($datas as $item){
                            $data = Admin::find($item);
                            if (!is_null($data) && $data->username!='root'){
                                $data->delete();
                            }
                        }
                        break;
                }
                break;
            case 'member':
                switch ($request->input('type')){
                    case 1:
                        foreach ($datas as $item){
                            $data = Member::find($item);
                            if (!is_null($data)){
                                $data->status = 1;
                                $data->save();
                            }
                        }
                        break;
                    case 2:
                        foreach ($datas as $item){
                            $data = Member::find($item);
                            if (!is_null($data)){
                                $data->status = 0;
                                $data->save();
                            }
                        }
                        break;
                    case 5:
                        $data = \DB::select("select * from member_false_datas where member_id = {$request->input('ids')} limit 1");
                        if(empty($data)){
                            \DB::insert('insert into member_false_datas (applys_num, member_id,created_at,updated_at) values (?, ?,?,?)', [$request->input('data'),$request->input('ids'),date('Y-m-d H:i:s'),date('Y-m-d H:i:s')]);
                        }else{
                            \DB::update('update member_false_datas set applys_num = ?,updated_at=? where member_id = ?', [$request->input('data'),date('Y-m-d H:i:s'),$request->input('ids')]);
                        }
                        break;
                    case 6:
                        $data = \DB::select("select * from member_false_datas where member_id = {$request->input('ids')} limit 1");
                        if(empty($data)){
                            \DB::insert('insert into member_false_datas (apply_amount, member_id,created_at,updated_at) values (?, ?,?,?)', [$request->input('data'),$request->input('ids'),date('Y-m-d H:i:s'),date('Y-m-d H:i:s')]);
                        }else{
                            \DB::update('update member_false_datas set apply_amount = ?,updated_at=? where member_id = ?', [$request->input('data'),date('Y-m-d H:i:s'),$request->input('ids')]);
                        }
                        break;
                }
                break;
            case 'news':
                switch ($request->input('type')){
                    case 1:
                        foreach ($datas as $item){
                            $data = News::find($item);
                            if (!is_null($data)){
                                $data->status = 1;
                                $data->save();
                            }
                        }
                        break;
                    case 2:
                        foreach ($datas as $item){
                            $data = News::find($item);
                            if (!is_null($data)){
                                $data->status = 0;
                                $data->save();
                            }
                        }
                        break;
                    case 3:
                        foreach ($datas as $item){
                            $data = News::find($item);
                            if (!is_null($data)){
                                $data->delete();
                            }
                        }
                        break;
                    case 4:
                        $data = News::find($request->input('ids'));
                        if (!is_null($data)){
                            $data->sort_at = $request->input('sort_at');
                            $data->save();
                        }
                        break;
                }
                break;
            case 'product':
                switch ($request->input('type')){
                    case 1:
                        foreach ($datas as $item){
                            $data = Product::find($item);
                            if (!is_null($data)){
                                $data->status = 1;
                                $data->save();
                            }
                        }
                        break;
                    case 2:
                        foreach ($datas as $item){
                            $data = Product::find($item);
                            if (!is_null($data)){
                                $data->status = 0;
                                $data->save();
                            }
                        }
                        break;
                    case 3:
                        foreach ($datas as $item){
                            $data = Product::find($item);
                            if (!is_null($data)){
                                $data->delete();
                            }
                        }
                        break;
                    case 4:
                        $data = Product::find($request->input('ids'));
                        if (!is_null($data)){
                            $data->sort_at = $request->input('sort_at');
                            $data->save();
                        }
                        break;
                }
                break;
            default:
                break;
        }

        if($request->ajax()) {
            return response()->json([
                'status' => 1,
                'message' => '操作成功'
            ]);
        }else{
            return redirect()->back()->withMessageSuccess('操作成功。');
        }
	}
}
